package ltg.ibutton.hydroroom.gui.tablet;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.ResultSet;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingConstants;
import javax.swing.table.DefaultTableCellRenderer;

import ltg.ibutton.core.FactorySimulation;
import ltg.ibutton.core.Simulation;
import ltg.ibutton.hydroroom.HydroRoomPersistence;

public class TabletGui extends JFrame implements ActionListener {
	private static final long serialVersionUID = 1L;
	
	private JButton confirmButton = new JButton("Confirm");
	private LeftPanel leftTopPanel = null;
	private TableDataModel tdm = null;
	private JTable table = null;
	
	public TabletGui() {
		super();
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setUndecorated(true);

        GraphicsDevice gd = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();
        gd.setFullScreenWindow(this);
		
		
		JPanel leftPanel = new JPanel();
		leftPanel.setLayout(new BorderLayout());
		
		leftTopPanel = new LeftPanel(confirmButton);
		leftPanel.add(leftTopPanel, BorderLayout.CENTER);
		
			JPanel confirmPanel = new JPanel();
			confirmPanel.setBorder(BorderFactory.createEmptyBorder(0, 0, 10, 0));
					
				confirmButton.addActionListener(this);
				confirmButton.setEnabled(false);
				confirmButton.setMinimumSize(new Dimension(300, 70));
				confirmButton.setMaximumSize(new Dimension(300, 70));
				confirmButton.setPreferredSize(new Dimension(300, 70));
				confirmPanel.add(confirmButton);
		
		leftPanel.add(confirmPanel, BorderLayout.SOUTH);
				
		tdm = new TableDataModel();
		table = new JTable(tdm);
		table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF); 
		table.setPreferredScrollableViewportSize(new Dimension(340, 768));
		//table.setFillsViewportHeight(true);
		updateTable();

        //Create the scroll pane and add the table to it.
        JScrollPane scrollPane = new JScrollPane(table);

        this.getContentPane().setLayout(new BorderLayout());
		this.getContentPane().add(leftPanel, BorderLayout.CENTER);
		this.getContentPane().add(scrollPane, BorderLayout.EAST);
		
		this.setSize(1024,768);
		//this.pack();
        this.setVisible(true);
        
        HydroRoomPersistence.closeEverything();
        
        //Model
        Simulation sim = FactorySimulation.getSim();
        sim.addObserver(leftTopPanel);
	}

	public void actionPerformed(ActionEvent e) {
		if(e.getSource() == confirmButton) {
			leftTopPanel.confirmAction(this);
		}
	}
	
	public void updateTable() {
		ResultSet rs = HydroRoomPersistence.getActionsByStation();
		tdm.setResultSet(rs);
		
		DefaultTableCellRenderer r = new DefaultTableCellRenderer();
		r.setHorizontalAlignment(SwingConstants.CENTER); //center the content of the cells
		
		table.getColumnModel().getColumn(0).setCellRenderer(r);
        table.getColumnModel().getColumn(0).setPreferredWidth(140); //type
        
        table.getColumnModel().getColumn(1).setCellRenderer(r);
        table.getColumnModel().getColumn(1).setPreferredWidth(140); //time
        
        table.getColumnModel().getColumn(2).setCellRenderer(r);
        table.getColumnModel().getColumn(2).setPreferredWidth(60); //coords
	}
}